$(function () {
    var start = {
        elem: '#startDate',
        format: 'YYYY-MM-DD hh:mm:ss',
        istime: true,
        choose: function (datas) {
            end.min = datas; //开始日选好后，重置结束日的最小日期
            end.start = datas //将结束日的初始值设定为开始日
        }
    };
    var end = {
        elem: '#endDate',
        format: 'YYYY-MM-DD hh:mm:ss',
        istime: true,
        choose: function (datas) {
            start.max = datas; //结束日选好后，重置开始日的最大日期
        }
    };
    laydate(start);
    laydate(end);
    //高级搜索
    $('body').on('click','.updateStatus',function () {
        var status= $(this).data("status");
        var id=$(this).data("id");
        var obj  =$(this);
        if(status=='FROZEN'){
            icModale(2,"确认冻结用户么？",0,function () {
                editAccount(id,status,obj);
            });
        }else if(status=='ACTIVE'){
            icModale(2,"确认解冻用户么？",0,function () {
                editAccount(id,status,obj);
            });
        }
    })
    $('body').on('click','.updateInfoStatus',function () {
        var status= $(this).data("status");
        var id=$(this).data("id");
        var obj  =$(this);
        if(status=='ACTIVE'){
            icModale(2,"确认解冻用户么？",0,function () {
                updateInfoStatus(id,status,obj);
            });
        }
    })
})
/**
 *冻结  解冻账户
 * @param id
 * @param status
 */
function updateInfoStatus(id, status,obj) {
    $.ajax({
        url: baseloaction+"/admin/user/cus/updateUserAccountStatus",
        data: {"status": status,"id":id},
        type: "post",
        dataType: "json",
        success: function (result) {
            if (result.success == true) {
                icModale(0,result.message);
                    $('#accountItem_'+id).removeClass('stu-s-accountItem');
                    $('.stu-s-accountBg').remove();
                    obj.remove();
            } else {
                icModale(0,result.message);
            }
        },
        error:function (error) {
            alert("系统错误，操作失败");
        }
    });
}




/**
 *冻结  解冻账户
 * @param id
 * @param status
 */
function editAccount(id, status,obj) {
    $.ajax({
        url: baseloaction+"/admin/user/cus/updateUserAccountStatus",
        data: {"status": status,"id":id},
        type: "post",
        dataType: "json",
        success: function (result) {
            if (result.success == true) {
                icModale(0,result.message);
                   if(status=='FROZEN'){
                       obj.data('status','ACTIVE');
                       obj.html('解冻账户');
                       $('#accountItem_'+id).addClass('stu-s-accountItem');
                       $('#accountItem_'+id).append(' <div class="stu-s-accountBg"><i class="fa fa-unlock-alt" title="账户冻结"></i></div>');
                   }else{
                       obj.data('status','FROZEN');
                       obj.html('冻结账户');
                       $('#accountItem_'+id).removeClass('stu-s-accountItem');
                   }
            } else {
                icModale(0,result.message);
            }
        },
        error:function (error) {
            alert("系统错误，操作失败");
        }
    });
}



//操作账户金额
//type=1银行充值，type=2银行冻结金额,type=3银行解冻金额
//type=4充值卡入账,type=5充值卡冻结金额,,type=6充值卡解冻金额,
//type=7银行扣款,type=8充值卡扣款
//表格中单个字段的修改
$('body').on('click', '.userAccount', function() {
    var id= $(this).data('id');
    var type= $(this).data('status');
    var message = "";
    if(type==1){
        message="请输入要充值的银行金额:";
    }else if(type==2){
        message="请输入要冻结的银行金额:";
    }else if(type==3){
        message="请输入要解冻的银行金额:";
    }else if(type==4){
        message="请输入要充值的充值卡金额:";
    }else if(type==5){
        message="请输入要冻结的充值卡金额:";
    }else if(type==6){
        message="请输入要解冻的充值卡金额:";
    }else if(type==7){
        message="请输入要扣除的银行金额:";
    }else if(type==8){
        message="请输入要扣除的充值卡金额:";
    }
    var index = parent.layer.open({
        type: 1,
        area: ['380px'],
        skin: 'layui-layer-demo',
        title: message,
        content: '<div class="m-t m-r m-l m-b"><input class="form-control"   id="amount" name="" type="text"></div>',
        btn: '确定',
        yes: function(index, layero) {
            var amount = layero.find("#amount").val();
            if(amount!=null){
                var reg =/^[0-9]+([\.]{1}[0-9]{1,2})?$/;
                if(!reg.test(amount)){
                    icModale(0,"金额必须是数字，且最多有2位小数");
                    return
                }
            }
            $.ajax({
                url:baseloaction+"/admin/user/cus/updateUserAccountAmount",
                type:"post",
                data:{'id':id,'type':type,'amount':amount},
                dataType:"json",
                success:function (result) {
                    parent.layer.close(index);
                    if (result.success == true) {
                        var entity = result.entity;
                        $("#balance_"+id).text(entity.balance);
                        $("#cashAmount_"+id).text(entity.cashAmount);
                        $("#cashForzenAmount_"+id).text(entity.cashForzenAmount);
                        $("#vmAmount_"+id).text(entity.vmAmount);
                        $("#vmForzenAmount_"+id).text(entity.vmForzenAmount);
                        icModale(0,result.message);
                    } else {
                        icModale(0,result.message);
                    }
                },
                error:function (result) {
                    parent.layer.close(index);
                    icModale(0,result.message);
                }
            });
        }
    });
});
